
<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
          <el-button type="success" size="mini" icon="el-icon-upload2" @click="onBatchAdd">批量导入</el-button>
          <el-button type="success" size="mini" icon="el-icon-upload2" @click="onDeleteAll">清空学生</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column align="center" label="学号" prop="student_no" width="120px" />
            <el-table-column align="center" label="姓名" prop="name" width="120px" />
            <el-table-column align="center" label="性别" prop="gender" width="80px" />
            <el-table-column align="center" label="年级" prop="grade" width="100px">
              <template #default="scope">
                {{ scope.row.grade}} 年级
              </template>
            </el-table-column>
            <el-table-column align="center" label="班级" prop="class_name" width="100px" />
            <el-table-column align="center" label="家长" prop="parent.name" width="120px" />
            <el-table-column align="center" label="状态" prop="status" width="80px">
              <template #default="scope">
                <el-tag type="success" v-if="scope.row.status === 1">正常</el-tag>
                <el-tag type="info" v-else>禁用</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="center" label="创建时间" prop="create_time" width="160px" />
            <el-table-column label="操作" fixed="right" width="150px">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>

      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
    <Import ref="importRef" @import-success="initData" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";
import Import from "./import";
export default {
  name: "index",
  components: {
    Add,
    Import
  },
  mixins: [crud],
  data() {
    return {
      request: http.student,
      searchList: [
        {
          label: '学号',
          prop: 'student_no',
          name: 'input',
          clearable: true
        },
        {
          label: '姓名',
          prop: 'name',
          name: 'input',
          clearable: true
        },
        {
          label: '年级',
          prop: 'grade',
          name: 'select',
          clearable: true,
          options: [
            { label: '一年级', value: 1 },
            { label: '二年级', value: 2 },
            { label: '三年级', value: 3 },
            { label: '四年级', value: 4 },
            { label: '五年级', value: 5 },
            { label: '六年级', value: 6 }
          ]
        },
        {
          label: '班级',
          prop: 'class_name',
          name: 'input',
          clearable: true
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          clearable: true,
          options: [
            { label: '正常', value: 1 },
            { label: '禁用', value: 0 }
          ]
        }
      ],
      searchParams: {
        student_no: '',
        name: '',
        grade: '',
        class_name: '',
        status: ''
      },
      tableData: []
    }
  },
  methods: {
    onBatchAdd() {
      this.$refs.importRef.open()
    },
    onDeleteAll() {
      this.$confirm('确认删除全部学生以及对应的选课？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
          .then(() => {
            this.request.deleteAll().then(res => {
              this.$message.success('删除成功')
              this.onSearch()
            })
          })
          .catch(() => {

          })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
